package com.atguigu.tingshu.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.tingshu.account.client.UserAccountFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.logins.environment.LoginClient;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.handler.codec.mqtt.MqttMessageIdAndPropertiesVariableHeader;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

   @Autowired
   private LoginClient loginClient;

//    @GlobalTransactional
    @Override
    public Map<String, Object> wxLogin(String code, String type) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code",code);
        return loginClient.login(type,paramMap);
    }

    @Override
    public UserInfoVo getUserInfo() {
        UserInfoVo userInfoVo = new UserInfoVo();
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = this.getById(userId);
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return userInfoVo;
    }

    @Override
    public void updateUser(UserInfoVo userInfoVo) {
        UserInfo userInfo = new UserInfo();
        Long userId = AuthContextHolder.getUserId();
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfo.setId(userId);
        this.updateById(userInfo);
    }

    @Override
    public void updateVipState() {
        //获取当天的零点
        LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        Date date = Date.from(of.atZone(ZoneId.systemDefault()).toInstant());
        UserInfo userInfo = new UserInfo();
        userInfo.setIsVip(0);
        this.update(userInfo,new LambdaUpdateWrapper<UserInfo>()
                .eq(UserInfo::getIsVip,1)
                .le(UserInfo::getVipExpireTime,date));
    }

    public static void main(String[] args) {
        LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        Date date = Date.from(of.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
    }
}
